package vm;

/**
 * @author zzb
 * @since 2021-01-20
 */
public class JavaVMStackOOM {
  private void dontStop() {
    while (true) {
    }
  }

  public void stackLeakByThread() {
    while (true) {
      Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
          dontStop();
        }
      });
      thread.start();
    }
  }

  public static void main(String[] args) {
    JavaVMStackOOM javaVMStackOOM = new JavaVMStackOOM();
    javaVMStackOOM.stackLeakByThread();
  }
  /**
   * [26.725s][warning][os,thread] Failed to start thread - _beginthreadex failed (EACCES) for attributes: stacksize: 2048k, flags: CREATE_SUSPENDED STACK_SIZE_PARAM_IS_A.
   * Exception in thread "main" java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
   * 	at java.base/java.lang.Thread.start0(Native Method)
   * 	at java.base/java.lang.Thread.start(Thread.java:803)
   * 	at vm.JavaVMStackOOM.stackLeakByThread(JavaVMStackOOM.java:21)
   * 	at vm.JavaVMStackOOM.main(JavaVMStackOOM.java:27)
   */
}